import urllib.request as ur
import urllib.parse as up
import json
while True:
    # 输入你要翻译的单词
    kw=(input('请输入翻译的内容：'))

    data={
        'kw':kw
    }
    #将data作为URL查询字符串
    data_url=up.urlencode(data)

    #封装Request对象
    request = ur.Request(
        url = 'https://fanyi.baidu.com/sug',
        data = data_url.encode('utf-8')#把url编码转成字节
    )
    # 获得查询结果
    response = ur.urlopen(request).read()
    #json对象转化为python对象
    ret = json.loads(response)
    print(ret)

    # 增加代码健硕性
    try:

        keys = ret['data'][0]['k']
        values = ret['data'][0]['v']
        print(keys,values)
    except:
        print('未查到相关结果，请规范您的输入！')
